<script lang="ts" setup name="XtxCarousel">
import { BannerItem } from '@/types/data.d'
import { ref, onMounted, onBeforeUnmount } from 'vue'
// js写法接收数据
// import { PropType } from 'vue'
// const props = defineProps({
//   bannerList: {
//     type: Array as PropType<BannerItem[]>,
//     required: true
//   }
// })
// ts写法接收数据
const props = withDefaults(
  defineProps<{
    bannerList: BannerItem[]
    autoPlay?: boolean
    duration?: number
  }>(),
  {
    autoPlay: true,
    duration: 2000
  }
)

// 定义一个响应式数据对应当前显示图片的索引
const currIndex = ref(0)

// 左右点击切换
const toggle = (num: number) => {
  const result = currIndex.value + num
  if (result < 0) {
    // 如果结果小于0  让它为最后一张
    currIndex.value = props.bannerList.length - 1
  } else if (result > props.bannerList.length - 1) {
    // 如果大于最大值  让它为0
    currIndex.value = 0
  } else {
    currIndex.value = result
  }
}
// 开始轮播
let interval = -1
const play = () => {
  if (!props.autoPlay) {
    return
  }
  //  setInterval nodejs,返回值就是Timer  浏览器js内就是number
  stop()
  interval = window.setInterval(() => {
    toggle(1)
  }, props.duration)
}
// 停止轮播
const stop = () => {
  clearInterval(interval)
}
// 渲染完成后执行轮播
onMounted(() => {
  play()
})
// 组件销毁前善后工作
onBeforeUnmount(() => {
  stop()
})
</script>
<script lang="ts">
export default {
  name: 'XtxCarousel'
}
</script>

<template>
  <div class="xtx-carousel" @mouseenter="stop" @mouseleave="play">
    <ul class="carousel-body">
      <li
        class="carousel-item"
        :class="{ fade: index === currIndex }"
        v-for="(item, index) in bannerList"
        :key="item.id"
      >
        <RouterLink to="/">
          <img :src="item.imgUrl" alt="" />
        </RouterLink>
      </li>
    </ul>
    <a href="javascript:;" class="carousel-btn prev" @click="toggle(-1)"
      ><i class="iconfont icon-angle-left"></i
    ></a>
    <a href="javascript:;" class="carousel-btn next" @click="toggle(1)"
      ><i class="iconfont icon-angle-right"></i
    ></a>
    <div class="carousel-indicator">
      <span
        :class="{ active: currIndex === index }"
        v-for="(item, index) in bannerList.length"
        :key="item"
        @mouseenter="currIndex = index"
      ></span>
    </div>
  </div>
</template>

<style scoped lang="less">
.xtx-carousel {
  width: 100%;
  height: 100%;
  min-width: 300px;
  min-height: 150px;
  position: relative;
  .carousel {
    &-body {
      width: 100%;
      height: 100%;
    }
    &-item {
      width: 100%;
      height: 100%;
      position: absolute;
      left: 0;
      top: 0;
      opacity: 0;
      transition: opacity 0.5s linear;
      &.fade {
        opacity: 1;
        z-index: 1;
      }
      img {
        width: 100%;
        height: 100%;
      }
    }
    &-indicator {
      position: absolute;
      left: 0;
      bottom: 20px;
      z-index: 2;
      width: 100%;
      text-align: center;
      span {
        display: inline-block;
        width: 12px;
        height: 12px;
        background: rgba(0, 0, 0, 0.2);
        border-radius: 50%;
        cursor: pointer;
        ~ span {
          margin-left: 12px;
        }
        &.active {
          background: #fff;
        }
      }
    }
    &-btn {
      width: 44px;
      height: 44px;
      background: rgba(0, 0, 0, 0.2);
      color: #fff;
      border-radius: 50%;
      position: absolute;
      top: 228px;
      z-index: 2;
      text-align: center;
      line-height: 44px;
      opacity: 0;
      transition: all 0.5s;
      &.prev {
        left: 20px;
      }
      &.next {
        right: 20px;
      }
    }
  }
  &:hover {
    .carousel-btn {
      opacity: 1;
    }
  }
}
</style>
